package com.proposition.commons.exam.util;

import com.proposition.commons.exam.exception.ExamImportException;
import com.proposition.commons.exam.result.ResultEnum;

import java.util.regex.Pattern;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 8:40 2018/1/15
 * @description:
 */
public class ExamFieldVolidateUtil {
    /**
     * @author: 陈玉林
     * @date: 2018/4/17 15:00
     * @description: 验证字段
     * 如果是题干 包含句号，就去掉句号
     */
    public static ExamImportException isIllegalField(String label, String field, String examNum) {
        if (field == null || field.length() == 0) {
            return new ExamImportException(label, examNum, ResultEnum.ILLEGAL_EXAM_FIELD);
        }
        if (field.length() > 250) {
            return new ExamImportException(label, examNum, ResultEnum.ILLEGAL_EXAM_FIELD);
        }
        return null;
    }

    /**
     * @author: 陈玉林
     * @date: 2018/4/17 16:26
     * @description: 递归法去掉题干结尾非数字、中文、字母的字符
     */
    public static String getIllegalStem(String stem) {
        //中文字母数字正则表达式
        String pattern = "^[A-Za-z0-9\\u4e00-\\u9fa5]+$";
        if (stem == null || stem.length() == 0) {
            return "";
        }
        //最后是括号设置为合法
        if (stem.substring(stem.length() - 1).equals("）")) {
            return stem;
        }
        if (stem.substring(stem.length() - 1).equals(")")) {
            return stem;
        }
        boolean isMatch = Pattern.matches(pattern, stem.substring(stem.length() - 1));
        if (isMatch) {
            return stem;
        }
        stem = stem.substring(0, stem.length() - 1);
        return getIllegalStem(stem);
    }

    public static void main(String[] args) {
        System.out.println(getIllegalStem("nsifk四@大峰会*框#，架.|@$$&&）"));
    }
}
